package com.yjf.response;



import java.util.HashMap;
import java.util.Map;

/**
 * CommonResponse构建器
 *
 * @author gaotx
 *
 */
public class CommonRespBuilder {

  private CommonResponse resp;

  /**
   * 创建操作成功共返回对象
   *
   * @return
   */
  public CommonRespBuilder createSuccessResp() {
    resp = new CommonResponse(ICommonResponse.SUCCESS_CODE, "操作成功");
    return this;
  }

  /**
   * 创建操作失败返回对象
   *
   * @return
   */
  public CommonRespBuilder createFailResp() {
    resp = new CommonResponse(ICommonResponse.FAIL_CODE, "操作失败");
    return this;
  }
  public CommonRespBuilder setMsg(String msg){
    resp.setMsg(msg);
    return this;
  }

  /**
   * 在commonresp中datas放入key，value值，用于个别无实体返回值，非线程安全！
   *
   * @param key
   * @param value
   * @return
   */
  @SuppressWarnings("unchecked")
  public CommonRespBuilder putDatas(String key, Object value) {
    if (resp.getDatas() == null) {
      Map<String, Object> datas = new HashMap<String, Object>();
      resp.setDatas(datas);
    }
    Map<String, Object> datas = (Map<String, Object>) resp.getDatas();
    datas.put(key, value);
    return this;
  }

  /**
   * 与电票统一，将page和total放入datas中
   *
   * @param obj
   * @param page
   * @param total
   * @return
   */
  @SuppressWarnings("unchecked")
  public CommonRespBuilder putPage(Object obj, int page, long total) {
    if (resp.getDatas() == null) {
      Map<String, Object> datas = new HashMap<String, Object>();
      resp.setDatas(datas);
    }
    Map<String, Object> datas = (Map<String, Object>) resp.getDatas();
    datas.put("pageList", obj);
    datas.put("page", page);
    datas.put("total", total);

    return this;
  }

  /**
   * 设置data数据
   *
   * @param obj
   * @return
   */
  public CommonRespBuilder setDatas(Object obj) {
    this.resp.setDatas(obj);
    return this;
  }

  /**
   * 返回构建的CommonResponse
   *
   * @return
   */
  public CommonResponse build() {
    return this.resp;
  }

  public static CommonRespBuilder builder(){
    return new CommonRespBuilder();
  }

}
